// packageA/pages/tixian/tixian.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    gglist:'',
    idx:0,
    idx1:0,
    idx2:0,
    list:[],
    list1:[],
    tixian:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   this.setData({
     idx:options.idx
   })
  },
  z_nav1(){
    this.setData({
      idx:0
    })
  },
  z_nav2(){
    this.setData({
      idx:1
    })
  },
  z_nav3(e){
    this.setData({
      gold_num:e.detail.value
    })
  },
  z_nav4(e){
    this.setData({
      idx1:e.currentTarget.dataset.id
    })
  },
  z_nav5(e){
    this.setData({
      idx2:e.currentTarget.dataset.id,
      pay_price:e.currentTarget.dataset.zfxj_num
    })
  },
  chongzhi() {
    let _this = this
    if (wx.getStorageSync("user_token") == "") {
      _this.z_dengluyanzeng()
    } else {
      let params = {
        "token":wx.getStorageSync("user_token"),
        "type":_this.data.idx2,
        "cz_id":_this.data.pay_price
      }
      wx.request({
        url: app.apiurl + 'api/recharge/memberRecharge',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          wx.requestPayment({
            // timeStamp: Date.parse(new Date()),
            timeStamp: res.data.data.timeStamp,
            nonceStr: res.data.data.nonceStr,
            package: res.data.data.package,
            signType: res.data.data.signType,
            paySign: res.data.data.paySign,
            success (res) { 
              // console.log("222222222222222",res)
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
              // _this.z_duihuan1()
            },
            fail (err) {
              console.log("33333333333",err)
            }
          })
        }
      })
    }
  },
  z_duihuan1(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
      "gold_num":_this.data.idx2
    }
    wx.request({
      url: app.apiurl + 'api/member/exchangeCashGold',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  z_duihuan2(){
    let _this = this
    if (wx.getStorageSync("user_token") == "") {
      _this.z_dengluyanzeng()
    } else {
      let params = {
        "token":wx.getStorageSync("user_token"),
        "tx_id":_this.data.idx1
      }
      wx.request({
        url: app.apiurl + 'api/member/txApply',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          
          if(res.data.code == '1007'){
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
            setTimeout(function(){
              wx.navigateTo({
                url: '/packageB/pages/meiri/meiri',
              })
            },2000)
          
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
            _this.setData({
              tixian:true
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.guanggao1()
    this.tixian()
    this.tixian1()
    this.z_yonghu()
    this.z_peizhi()
  },
  guanggao1(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advertPage',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
          console.log(res.data.data)
         _this.guanggao2()
      }
    })
  },
  guanggao2(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advert?page_id=13',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
         _this.setData({
           gglist:res.data.data[0]
         })
      }
    })
  },
  z_kvlianjia(e){
    let _this = this
    let link_url = e.currentTarget.dataset.url
    let id = e.currentTarget.dataset.url
    if(link_url != undefined){
      let params = {
        "token":wx.getStorageSync("user_token"),
        "id":id
      }
      wx.request({
        url: app.apiurl + 'api/member/signRecord',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
           wx.navigateTo({
             url: link_url,
           })
        }
      })
    }
  },
  tixian(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/txList',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
         _this.setData({
           list:res.data.data
         })
      }
    })
  },
  tixian1(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/czjbList',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
         _this.setData({
           list1:res.data.data
         })
      }
    })
  },
  z_yonghu(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/myInfo',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
       if(res.data.code == '1001'){
         _this.setData({
           yonghu:res.data.data
         })
       }
      }
    })
  },
  z_peizhi() {
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/configBase',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          peizhi:res.data.data
        })
      }
    })
  },
  z_dengluyanzeng(){
    wx.request({
      url: app.apiurl + 'api/home/xcxLoginStatus',
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        if (res.data.code == '1001') {
          if (res.data.data.is_force_login == '1') {
            // wx.reLaunch({
            //   url: '/pages/login/login'
            // });
            wx.showToast({
              title: '请注册/登录会员账号',
              icon: 'none',
              duration: 2000
            })
            setTimeout(function(){
              wx.reLaunch({
                url: '/pages/member/member',
              })
            },2500)
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})